import _pickle as cPickle
import pickle
import time

'''
pickle模块是python专有的，其他语言不能共享
cPickle是c语言实现、理论上比pickle(python自己实现的)快
'''


def p():
    pd = pickle.dumps(d)
    print(pd)
    pdd = pickle.loads(pd)
    print(pdd)


def c_p():
    pd = cPickle.dumps(d)
    print(pd)
    pdd = cPickle.loads(pd)
    print(pdd)


def bench_cp():
    start = time.time()
    for i in range(times):
        cPickle.loads(cPickle.dumps(d))
    print('cp operate %d times cost:%f' % (times, time.time() - start))


def bench_p():
    start = time.time()
    for i in range(times):
        pickle.loads(pickle.dumps(d))
    print('p operate %d times cost:%f' % (times, time.time() - start))


def p_file():
    with open('p.p', 'wb') as f:  # 字节流方式读写
        cPickle.dump(d, f)
    with open('p.p', 'rb') as f:
        dd = cPickle.load(f)
        print(dd)


if __name__ == '__main__':
    d = dict(name='Bob', age=20, score=88)  # = {'name':'Bob','age':20,'score':88}
    times = 10000000
    # p()
    # c_p()
    # p_file()
    bench_p()
    bench_cp()
